package com.example.mqspring.rabiitmqbasicleverone.rabbitmqBroadcast;

import com.example.mqspring.utils.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;

public class MainMethod {

    public static void main(String[] args) {

        String queue = "selfqueue";
        String myExchangge = "bobo-exchange";
        String message = "今天下午2点发工资";
//      接收订阅消息的队列
        String firstQue = "myfirstQueue";
        String twoQue = "mysecondQueue";
//        创建连接 信道
        Connection connection = RabbitMqUtil.createConnection();
        Channel channel = null;
        MessagerPublisher messagerPublisher = new MessagerPublisher();
        try {
            channel = connection.createChannel();
            //            发送  绑定队列
            messagerPublisher.sendMessage(channel,myExchangge,message,firstQue,twoQue);
//           接收第一个队列
            MessageReceiver messageReceiver = new MessageReceiver(channel,myExchangge);
//          接收第二个队列
            MessageReceiverTwo messageReceiverTwo = new MessageReceiverTwo(channel, myExchangge);
//            监听消费
            while (true) {
                String tag1 = channel.basicConsume(firstQue, true, messageReceiver);
                String tag2 = channel.basicConsume(twoQue,true,messageReceiverTwo);
                channel.basicCancel(tag1);
                channel.basicCancel(tag2);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
